<?php
Loader::import('pspframework.utils.sessions.PspSession');

class SessionComponent{

/**
用来控制是否开启session的功能
 */
	public $__active = true;
	
	public function __construct() {
		if (Param::read('Session.start') !== true) {
			$this->__active = false;
		}
	}

	public function startup(&$controller) {
		if ($this->started() === false && $this->__active === true) {
			PspSession::start();
		}
	}
/**
 * Get 或 Set the userAgent
 *
 * @param string $userAgent Set the userAgent
 * @return void
 */
	public function userAgent($userAgent = null) {
		return PspSession::userAgent($userAgent);
	}

/**

 *
 * 在controller: $this->Session->write('Controller.sessKey', 'session value');
 */
	public function write($name, $value = null) {
		return PspSession::write($name, $value);
	}

/**

 * 在controller: $this->Session->read('Controller.sessKey');
 */
	public function read($name = null) {
		return PspSession::read($name);
	}

/**
 * 在controller: $this->Session->delete('Controller.sessKey');
 */
	public function delete($name) {
		return PspSession::delete($name);
	}

/**
判断是否建立了该变量
 * 在controller: $this->Session->check('Controller.sessKey');
 */
	public function check($name) {
		return PspSession::check($name);
	}

/**
 * Used to determine the last error in a session.
 *
 * In your controller: $this->Session->error();
 *
 * @return string Last session error
 */
	public function error() {
		return PspSession::error();
	}

/**
 * Used to set a session variable that can be used to output messages in the view.
 *
 * In your controller: $this->Session->setFlash('This has been saved');
 *
 */
	public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
		PspSession::write('Message.' . $key, compact('message', 'element', 'params'));
	}

/**
 * 从新生出一个 session id
 * 在controller: $this->Session->renew();

 */
	public function renew() {
		return PspSession::renew();
	}

/**
 * Used to check for a valid session.
 *
 * In your controller: $this->Session->valid();
 *
 * @return boolean true is session is valid, false is session is invalid
 */
	public function valid() {
		return PspSession::valid();
	}
	
	public function returnSessionVars() {
		return PspSession::_returnSessionVars();
	}
/**
 * Used to destroy sessions
 *
 * In your controller: $this->Session->destroy();
 */
	public function destroy() {
		return PspSession::destroy();
	}

/**
 * Get或Set the session id.

 */
	public function id($id = null) {
		if (empty($id)) {
			PspSession::start();
		}
		return PspSession::id($id);
	}


	public function started() {
		return PspSession::started();
	}

}
?>